<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>数组算法-去重</title>
</head>
<body>
<script>
console.log('----------------------去重1----------------------');
Array.prototype.unique1 = function () {
	return Array.from(new Set(this));
};
console.log([2, 2, 3, 3, 3, 4, 4].unique1());
</script>
<script>
console.log('----------------------去重2----------------------');
Array.prototype.unique2 = function () {
	const arr1 = this;
	const arr2 = [];
	const obj = {};
	arr1.forEach((item, index) => {
		if(!obj[item]) {
			arr2.push(item);
		}
		obj[item] = true;
	});
	return arr2;
};
console.log([2, 2, 3, 3, 3, 4, 4].unique2());
</script>
<script>
console.log('----------------------去重3----------------------');
Array.prototype.unique3 = function () {
	const arr1 = this;
	const arr2 = [];

	arr1.forEach((item, index) => {
		if(arr1.indexOf(item, index + 1) == -1) {
			arr2.push(item);
		}
	});
	return arr2;
};
console.log([2, 2, 3, 3, 3, 4, 4].unique3());
</script>
<script>
console.log('----------------------去重4----------------------');
Array.prototype.unique4 = function () {
	const arr1 = this;
	const arr2 = [];

	arr1.forEach((item, index) => {
		if(!arr2.includes(item)) {
			arr2.push(item);
		}
	});
	return arr2;
};
console.log([2, 2, 3, 3, 3, 4, 4].unique4());
</script>
<script>
console.log('----------------------去重5----------------------');
Array.prototype.unique5 = function () {
	const arr1 = this;
	const arr2 = [];

	arr1.forEach((item, index) => {
		if(!arr2.includes(item)) {
			arr2.push(item);
		}
	});
	return arr2;
};
console.log([2, 2, 3, 3, 3, 4, 4].unique5());
</script>
</body>
</html>